 /**
 * Description: 角色权限业务实现类
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-10 下午 19:40:19
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-10   xk       0.0.1     Initial
 */
package com.metro.activation.sys.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.activation.sys.mapper.ISysRoleMenuMapper;
import com.metro.activation.sys.service.inf.ISysRoleMenuService;
import com.metro.activation.sys.beans.vo.SysRoleMenu;

import java.util.List;

 /**
 * 角色权限业务实现类<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-10]
 */
@Service("sysRoleMenuService")
public class SysRoleMenuService extends BaseServiceSupport<SysRoleMenu> implements ISysRoleMenuService{
    //角色权限Mapper
    @Autowired
    private ISysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public IMapper getMapper() {
        return sysRoleMenuMapper;
    }

    @Override
    public IResult save(SysRoleMenu entity) {
        try {

            List<SysRoleMenu> list = entity.buildRoleMenus();
            if(CollectionUtil.isNotEmpty(list)){
                //删除角色现有的权限
                sysRoleMenuMapper.deleteByRoleId(entity);
                sysRoleMenuMapper.batchSaveRoleMenu(list);
            }
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增角色权限失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(SysRoleMenu entity) {
        try {
            sysRoleMenuMapper.update(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改角色权限失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            sysRoleMenuMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            sysRoleMenuMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
}